// pages/submitCases/submitCases.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    patientName: '', //病人姓名
    pAddress: ['广东省', '广州市', '番禺区'], //通讯地址
    pAge: '', //年龄
    pSex: '', //性别
    pPhone: '', //联系方式

    degreeArr: ['暂无', '轻微', '一般', '严重', '紧急'], //病情程度的数组
    degree: '暂无', //病情程度
    degreeId: 0, //病情程度的选择器变量

    conditionArr: ['确诊', '治愈', '死亡'], //当前状态的数组
    condition: '确诊', //当前状态
    conditionId: 0, //当前状态的选择器变量

    illness: '', //疾病类型
    illDate: '2020-01-01', //发病时间
    symptom: '', //发病症状
    remarks: '' //备注信息

  },
  // 联系方式选择器
  bindRegionChange: function(e) {
    // console.log('pAddress:', e.detail.value)
    this.setData({
      pAddress: e.detail.value
    })
  },
  // 病情程度选择器
  degreePicker(e) {
    var degreeId = e.detail.value
    var degreeArr = this.data.degreeArr
    var degree = degreeArr[degreeId]
    // console.log("degree:" + degree)
    this.setData({
      degreeId: degreeId,
      degree: degree

    })
  },
  // 当前状态选择器
  conditionPicker(e) {
    var conditionId = e.detail.value
    var conditionArr = this.data.conditionArr
    var condition = conditionArr[conditionId]
    // console.log("conditon："+condition)
    this.setData({
      conditionId: conditionId,
      condition: condition
    })

  },
  // 发病时间选择器
  bindDateChange(e){
    // console.log(e)
    var illDate=e.detail.value
    this.setData({
      illDate:illDate
    })
  },
  // 保存表单
  saveForm(e) {
    var that=this
    // 利用本地存储保存该表单内容
    var form = {}
    form['patientName'] = that.data.patientName
    form['pAge'] = that.data.pAge
    form['pAddress'] = that.data.pAddress
    form['pSex'] = that.data.pSex
    form['pPhone'] = that.data.pPhone
    form['degree'] = that.data.degree
    form['condition'] = that.data.condition
    form['illness'] = that.data.illness
    form['illDate'] = that.data.illDate
    form['symptom'] = that.data.symptom
    form['remarks'] = that.data.remarks
    wx.setStorage({
      key: 'form',
      data: form,
      success(res){
        console.log("form表单保存到本地成功",form)
        wx.showToast({
          title: '保存成功！',
          icon:'None',
        })
      },
      fail(res){
        wx.showToast({
          title: '保存失败！',
          icon: 'None',
        })
      }
    })
  },
  // 提交案理表单
  submitForm(e) {
    var that=this
    wx.showModal({
      title: '提交',
      content: '确定提交该案例吗？',
      success: function(res) {
        if (res.confirm) {
          // 在这验证数据非空和发送请求到后台
          console.log(e)
          // 提交完成后清空所有
          that.emptyAll()
        }
      }
    })
  },
  // input框监听函数
  inputLine(e){
    var inputLine=e.currentTarget.dataset.id
    var str=e.detail.value
    this.setData({
      [inputLine]: str
    })
  },
  // 清空所有 函数
  emptyAll(){
    this.setData({
      patientName: '', //病人姓名
      pAddress: ['广东省', '广州市', '番禺区'], //通讯地址
      pAge: '', //年龄
      pSex: '', //性别
      pPhone: '', //联系方式
      degreeArr: ['无', '轻微', '一般', '严重', '紧急'], //病情程度的数组
      degree: '无', //病情程度
      degreeId: 0, //病情程度的选择器变量
      conditionArr: ['确诊', '治愈', '死亡'], //当前状态的数组
      condition: '确诊', //当前状态
      conditionId: 0, //当前状态的选择器变量
      illness: '', //疾病类型
      illDate: '2020-01-01', //发病时间
      symptom: '', //发病症状
      remarks: '' //备注信息
    })
    // 更新本地缓存，防止再进入时为旧的内容
    this.saveForm1()
  },
  // 清空单行input栏函数
  emptyLine(e) {
    console.log(e.currentTarget)
    var inputLine = e.currentTarget.id
    this.setData({
      [inputLine]: ''
    })
  },
  // 更新本地缓存表单
  saveForm1() {
    var that = this
    // 利用本地存储保存该表单内容
    var form = {}
    form['patientName'] = that.data.patientName
    form['pAge'] = that.data.pAge
    form['pAddress'] = that.data.pAddress
    form['pSex'] = that.data.pSex
    form['pPhone'] = that.data.pPhone
    form['degree'] = that.data.degree
    form['condition'] = that.data.condition
    form['illness'] = that.data.illness
    form['illDate'] = that.data.illDate
    form['symptom'] = that.data.symptom
    form['remarks'] = that.data.remarks
    wx.setStorage({
      key: 'form',
      data: form,
    })

  },
  // onShow函数
  onLoad(e){
    var that=this
    wx.getStorage({
      key: 'form',
      success(res) {
        console.log("获取缓存成功",res.data)
        for(i in res.data){
          that.setData({
            [i]: res.data[i]
          })
        }
        
      }
    })
  }

})